<?php
namespace app\index\controller;

use think\Db;

class Index extends Base{
    protected $userId;
    public function _initialize(){
        parent::_initialize();
        $this->userId=cookie('user_id');
    }
    public function index(){
        $user=Db::name('users')->where('id',$this->userId)->find();
        $this->assign([
            'user' => $user,
            'notice' => Db::name('notice')->field('title')->select(),
        ]);
        return view();
    }
    // 我的旷工
    public function myWorker(){
        if (request()->isAjax()){
            $page=input('page');
            $pageSize=input('pagesize');
            $myWorker=Db::name('user_worker')->where('user_id',$this->userId)->page($page,$pageSize)->field('worker_id,estimated_income,current_income')->select();
            foreach ($myWorker as $key => $value){
                $info=getWorkerInfo($value['worker_id']);
                $myWorker[$key]['img']=$info['img'];
                $myWorker[$key]['name']=$info['name'];
            }
            return json(['msg' => '获取成功' ,'list' => $myWorker]);
        }
        return view('my_worker');
    }
    // 旷工详情
    public function workerDetail(){
        return view('worker_detail');
    }
    // 账户明细
    public function detail(){
        if (request()->isAjax()){
            $page=input('page');
            $pagesize=input('pagesize');
            $list=Db::name('account_detail')->where('user_id',$this->userId)->order('add_time desc')->page($page,$pagesize)->select();
            foreach ($list as $key => $value){
                $list[$key]['time']=date('Y-m-d H:i:s',$value['add_time']);
                $list[$key]['img']='/static/img/out.png';
                if ($value['money'] > 0){
                    $list[$key]['img']='/static/img/in.png';
                }
            }
            return json(['msg' => '获取成功','list' => $list]);
        }
        $expenditure=Db::name('account_detail')->where('user_id',$this->userId)->order('add_time desc')->where('money','<',0)->limit(15)->select();
        $this->assign([
            'expenditure' => $expenditure,
            'income' => Db::name('account_detail')->order('add_time desc')->where('user_id',$this->userId)->where('money','>',0)->limit(15)->select(),
        ]);
        return view();
    }
    // 我的团队
    public function myTeam(){
        if (request()->isAjax()){
            $page=input('page');
            $pageSize=input('pagesize');
            $field='reg_time,share_level,level,nickname,avatar';
            $pList=Db::name('users')->where('p_id',$this->userId)->page($page,$pageSize)->order('reg_time desc')->field($field)->select(); // 直推的团队
            foreach ($pList as $key => $value){
                $pList[$key]['reg_time']=date('Y-m-d',$value['reg_time']);
                $pList[$key]['level']=get_share_name($value['share_level']);
                if ($value['share_level'] <= 0){
                    $pList[$key]['level']=get_level_name($value['level']);
                }
            }
            return json(['msg' => 'ok','list' => $pList]);
        }
        $teamCount=Db::name('users')->whereLike('p_list',"%$this->userId%")->count('id'); // 团队人数
        $teamReward=Db::name('team_reward')->whereLike('p_list',"%$this->userId%")->sum('reward'); // 团队业绩
        $this->assign([
            'pList' => [],
            'teamCount' => $teamCount,
            'teamReward' => $teamReward,
        ]);
        return view('my_team');
    }
    // 推荐好友
    public function share(){
        $this->assign('userId',$this->userId);
        return view();
    }
}
